//
//  DataManager+CreateTable.m
//  ELibary
//
//  Created by luoz on 15/8/12.
//  Copyright (c) 2015年 luoz. All rights reserved.
//

#import "DataManager+CreateTable.h"

@implementation DataManager (CreateTable)

- (BOOL)isHasTableInSqliteWithTableName:(NSString *)tableName
{
    NSString * sql = [NSString stringWithFormat:@"SELECT COUNT(*) as c FROM sqlite_master where type='table' and name='%@'",tableName];
    FMResultSet * rs =[db executeQuery:sql];
    int count = 0;
    while ([rs next]){
        count = [[rs stringForColumn:@"c"] intValue];
    }
    
    if (count == 0) {
        return NO;
    }else{
        return YES;
    }
}

- (BOOL)createItemsTable
{
    if([self isHasTableInSqliteWithTableName:@"items"]){
        return YES;
    }
    else{
        NSString *sql = @"CREATE  TABLE 'items' ('index' INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , 'name' VARCHAR, 'num' INTEGER, 'icon' BLOB)";
        BOOL isSuccess = [db executeUpdate:sql];
        return isSuccess;
    }
}

@end
